package com.rm.zeyou.util.login;

import com.rm.zeyou.entity.User;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.UserMapper;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class LoginUtil {
    @Resource
    private UserMapper userMapper;


    public Map<String, String> getToken(Long id) {
        //先校验是否封禁
        StpKit.USER.checkDisable(id);
        // 在当前会话登录 用户 账号
        StpKit.USER.login(id);
        String tokenValue =  StpKit.USER.getTokenValue();
        String tokenName =  StpKit.USER.getTokenName();
        HashMap<String, String> map = new HashMap<>();
        map.put("tokenName", tokenName);
        map.put("tokenValue", tokenValue);
        return map;
    }

    public User getUser() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        User user = userMapper.selectById(userId);
        if (CheckEmptyUtil.isEmpty(user)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.USER_LOGIN_ERROR.getCode(), ErrorCodeExceptionEnum.USER_LOGIN_ERROR.getMsg());
        }
        return user;
    }

}
